<template>
	<view class="container">
		<view class="top_box">
			<view class="search_box">
				<u-icon name="search" size="40" color="#707070"></u-icon>
				<input type="text" v-model="searchQuery.keyword" placeholder="搜索内容" />
				<view class="search_button" @click="searchClick">搜索</view>
			</view>
		</view>
		<view class="history">
			<view class="title">历史搜索</view>
			<view class="keyword_list">
				<view class="keyword" v-for="(item,index) in keyArr" :key="index" @click="itemClick(item)">{{item}}
				</view>
			</view>
		</view>
		<view class="commodity_box">
			<view class="pu_box">
				<view class="list" v-for="item in goodsArr[0]" :key="item" @click="godetails(item)">
					<image :src="item.image" mode="widthFix"></image>
					<view class="text_box">
						<view class="name">{{item.store_name}}</view>
						<!-- <view class="remake">2222</view> -->
						<view class="price_text">
							<text class="big"><text>￥</text>{{item.price}}</text>
							<!-- <text class="small" v-if="item.ot_price"><text>￥</text>{{item.ot_price}}</text> -->
							<view class="addcart" @click.stop="goodsClick(item)">
								立即购买
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="pu_box">
				<view class="list" v-for="item in goodsArr[1]" :key="item" @click="godetails(item)">
					<image :src="item.image" mode="widthFix"></image>
					<view class="text_box">
						<view class="name">{{item.store_name}}</view>
						<!-- <view class="remake">2222</view> -->
						<view class="price_text">
							<text class="big"><text>￥</text>{{item.price}}</text>
							<!-- <text class="small" v-if="item.ot_price"><text>￥</text>{{item.ot_price}}</text> -->
							<view class="addcart" @click.stop="goodsClick(item)">
								立即购买
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="status" v-if="status !== 'nomore'" />
		<u-popup v-model="addCar" mode="bottom" border-radius="20" @close="maskClose" :closeable="true">
			<view class="con">
				<view class="top_img">
					<image :src="goodsDetail.content_banner[0].img" mode="widthFix"></image>
				</view>
				<view class="con_bg">
					<view class="mall_info">
						<image
							:src="skuValue[skuCurrent].image!==''?skuValue[skuCurrent].image:goodsDetail.storeInfo.image"
							mode=""></image>
						<view class="infos">
							<view class="name" style="font-weight: bold;font-size: 28rpx;">
								{{goodsDetail.storeInfo.store_name}}{{skuValue[skuCurrent].suk}}
							</view>
							<view class="specs">库存：{{skuValue[skuCurrent].stock}}</view>
							<view class="price">
								<view class="big"><text>￥</text>{{skuValue[skuCurrent].price}}</view>
								<view class="small"><text>￥</text>{{skuValue[skuCurrent].ot_price}}</view>
							</view>
						</view>
					</view>
					<view class="sku_box">
						<view class="title">规格</view>
						<scroll-view scroll-x="true">
							<view class="guigebox">
								<view class="list" v-for="(item,index) in skuValue" :key="index"
									:class="skuCurrent==index?'active':''" @click="skuClick(item,index)">
									<image :src="item.image||goodsDetail.storeInfo.image" mode=""></image>
									<view class="text"
										style="background: #fff !important;font-size: 24rpx;font-weight: normal;">
										{{item.suk}}
									</view>
									<view class="text" style="font-size: 20rpx;height: 40rpx;font-weight: normal;">
										￥{{item.price}}/瓶</view>
								</view>
							</view>
						</scroll-view>
					</view>
					<view class="sku_box">
						<view class="title">
							<view class="txt">冰冻</view>
							<view class="sku"><text style="margin-right: 10rpx;">共{{bdNumber + cwNumber}}瓶</text>|<text
									class="ice">冰冻{{bdNumber}}</text>常温{{cwNumber}}</view>
						</view>
						<view class="sku bing">
							<view class="lists" v-for="(item,index) in skuValue[skuCurrent].wdInfo" :key="index"
								v-if="item !== '自定义冰冻数量'" :class="bdCurrent==index?'active':''"
								@click="bdClick(item,index)">
								{{item}}
							</view>
						</view>
						<view class="input_box list" style="width: 100%;" :class="bdCurrent == 3 ? 'active' : ''"
							v-if="skuValue[skuCurrent].wdInfo.includes('自定义冰冻数量')||(skuValue[skuCurrent].wdInfo.length>1 && (bdNumber + cwNumber)>1)"
							@click="bdClick('自定义冰冻数量',3)">
							<view>自定义冰冻数量</view>
							<view style="flex: 1;display: flex;justify-content: space-between;padding-right: 20rpx;"
								v-if="bdCurrent == 3">
								<text style="font-weight: normal;margin-right: 20rpx;">全冰</text> <u-slider
									v-model="bfNumber" setup="1" height="10" style="flex: 1;"
									@moving="sliderEnd"></u-slider> <text
									style="font-weight: normal;margin-left: 20rpx;">不冰</text>
							</view>
						</view>
					</view>
					<view class="number">
						购买数量
						<u-number-box v-model="buyNumber" @change="valChange"
							:min="skuValue[skuCurrent].buy_num"></u-number-box>
					</view>
					<view class="btns">
						<view class="add_cart" @click="addCarClick('0')">加入购物车</view>
						<view class="buy_btn" @click="addCarClick('1')">立即购买</view>
					</view>
				</view>
			</view>

		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: [],
				keyArr: ['雪花', '百威', '原浆'],
				status: 'loadmore',
				isLogin: false,
				searchQuery: {
					page: 1,
					limit: 10,
					keyword: ''
				},
				goodsInfo: null,
				goodsDetail: null,

				skuValue: [{
					wdInfo: []
				}],
				selectWd: '全冰',
				selectSku: null,
				skuCurrent: 0,
				bdCurrent: 0,
				bdNumber: 0,
				bfNumber: 0,
				cwNumber: 0,
				buyNumber: 1,
				addCar: false,
				goodsArr: []
			}
		},
		onShow() {
			if (uni.getStorageSync('userToken')) {
				this.isLogin = true
			} else {
				this.isLogin = false
			}
		},
		methods: {
			bdClick(item, index) {
				this.bdCurrent = index
				this.selectWd = item
				this.valChange()
			},
			valChange() {
				if (this.selectWd == '全冰') {
					this.bdNumber = this.buyNumber * this.selectSku.num
					this.cwNumber = 0
				} else if (this.selectWd == '一半冰' || this.selectWd == '一半冰一半不冰') {
					this.bdNumber = Math.ceil(this.buyNumber * this.selectSku.num / 2)
					this.cwNumber = this.buyNumber * this.selectSku.num - this.bdNumber
				} else if (this.selectWd == '常温') {
					this.bdNumber = 0
					this.cwNumber = this.buyNumber * this.selectSku.num
				} else if (this.selectWd == '自定义冰冻数量') {
					this.sliderEnd()
				}
			},
			sliderEnd() {
				let cs = this.bfNumber / 100
				this.cwNumber = Math.ceil(this.buyNumber * this.selectSku.num * cs)
				this.bdNumber = this.buyNumber * this.selectSku.num - this.cwNumber
			},
			skuClick(item, index) {
				this.selectSku = item
				this.skuCurrent = index
				this.bdCurrent = 0
				this.bfNumber = 0
				this.buyNumber =this.skuValue[index].buy_num
				this.selectWd = this.skuValue[index].wdInfo[0]
				this.valChange()
			},
			addCarClick(type) {
				this.$allApi.addCar({
					productId: this.selectSku.product_id,
					cartNum: this.buyNumber,
					uniqueId: this.selectSku.unique,
					bd_value: JSON.stringify({
						bd: this.bdNumber,
						cw: this.cwNumber,
						type: this.selectWd,
						bfNumber: this.bfNumber / 100
					}),
					new: type
				}).then(res => {
					this.addCar = false
					this.bdNumber = 0
					this.cwNumber = 0
					this.buyNumber = 1
					this.bdCurrent = 0
					this.bfNumber = 0
					this.bdCurrent = 0
					this.selectWd = '全冰'
					if (type == '0') {
						this.$u.toast('添加成功')
					} else {
						this.$gourl(`/mallPage/confirmOrder?car_id=${res.data.cartId}`)
					}
				})
			},
			maskClose() {
				this.goodsDetail = null
				this.skuValue = []
				this.selectSku = null
				this.skuCurrent = 0
			},
			godetails(item) {
				this.$gourl('/mallPage/goodsDetails?id=' + item.id, 4)
			},
			goodsClick(item) {
				this.goodsInfo = item
				this.getGoodsData(item.id)
			},
			getGoodsData(id) {
				this.$allApi.getGoodsDetail({
					id: id
				}).then(res => {
					console.log(res);
					this.goodsDetail = res.data
					this.skuValue = []
					for (let item in res.data.productValue) {
						this.skuValue.push(res.data.productValue[item])
					}
					for (let item of this.skuValue) {
						item.wdInfo = []
						item.wdInfo = item.wd_value.split(',')
					}
					this.selectSku = this.skuValue[0]
					this.buyNumber = this.skuValue[0].buy_num
					this.selectWd = this.selectSku.wdInfo[0]
					this.valChange()
					this.addCar = true
				})
			},
			itemClick(item) {
				this.goodsList = []
				this.searchQuery = {
					page: 1,
					limit: 10,
					keyword: item
				}
				this.getGoodsList()
			},
			searchClick() {
				console.log(12);
				this.goodsList = []
				if (this.searchQuery.keyword == '') {
					console.log(123);
					return this.$u.toast('请输入搜索关键词')
				}
				if (!this.keyArr.includes(this.searchQuery.keyword)) { // 使用includes方法检查数组中是否已包含要添加的数据
					this.keyArr.unshift(this.searchQuery.keyword)
				}
				if (this.keyArr.length > 10) {
					this.keyArr.pop()
				}
				this.searchQuery.page = 1
				uni.setStorageSync('keyWords', JSON.stringify(this.keyArr))
				this.getGoodsList()
			},
			async getGoodsList() {
				await this.$allApi.getGoodsList(this.searchQuery).then(res => {
					this.goodsList = [...res.data, ...this.goodsList]

					if (res.data.length < this.searchQuery.limit) {
						this.status = 'nomore'
					} else {
						this.status = 'loadmore'
					}
					let arr1 = []
					let arr2 = []
					for (let i = 0; i < this.goodsList.length; i++) {
						if (i % 2) {
							arr2.push(this.goodsList[i])
						} else {
							arr1.push(this.goodsList[i])
						}
					}
					this.goodsArr = [arr1, arr2]
				})
			},
		},
		onReachBottom() {
			if (this.goodsList.length < this.searchQuery.limit || this.status == 'nomore') return;
			this.status = 'loading';
			this.searchQuery.page++
			this.getGoodsList()
		},
		onLoad() {
			if (uni.getStorageSync('keyWords')) {
				this.keyArr = JSON.parse(uni.getStorageSync('keyWords'))
			} else {
				this.keyArr = ['雪花', '百威', '原浆']
			}
		}
	}
</script>

<style>
	.u-drawer-bottom {
		background: none !important;
	}
</style>
<style scoped lang="scss">
	.top_img {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 20rpx 0;
	}

	.con {

		.con_bg {
			background: #F6F6F6;
			padding-bottom: calc(30rpx + constant(safe-area-inset-bottom));
			padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
		}

		.btns {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-radius: 45rpx;
			overflow: hidden;

			view {
				flex: 1;
				height: 90rpx;
				background: #000000;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
			}

			.buy_btn {
				background: #DB3E3F;
			}
		}

		.number {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx;
			border-top: 1px solid #f3f3f3;
			background: #fff;
			margin: 20rpx 0;
		}

		.sku_box {
			margin-top: 12rpx;
			padding: 0 20rpx;
			background: #fff;
			margin-top: 20rpx;
			padding: 20rpx;

			.input_box {
				display: flex;
				align-items: center;
				justify-content: flex-start;
				border: 1px solid #f6f6f6;
				border-radius: 10rpx;

				view {
					width: 236rpx;
					height: 60rpx;
					border-radius: 10rpx;
					background: #F6F6F6;
					display: flex;
					align-items: center;
					justify-content: center;
				}

				input {
					flex: 1;
					padding-left: 20rpx;
				}
			}

			.title {
				margin-bottom: 12rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-weight: bold;

				.sku {
					font-weight: normal;

					.ice {
						color: #26ACF5;
						margin: 0 10rpx;
					}
				}
			}

			.sku {
				display: flex;
				align-items: center;
				justify-content: flex-start;
				flex-wrap: wrap;

				.lists {
					border-radius: 10rpx;
					background: #F6F6F6;
					margin: 20rpx;
				}
			}

			.guigebox {

				display: flex;
				align-items: center;
				justify-content: flex-start;
				height: 242rpx;
				/* 添加以下样式，禁止父级元素拦截触摸事件 */
				-webkit-overflow-scrolling: touch;


				.list {
					margin: 10rpx;
					width: 142rpx;
					border-radius: 12rpx;
					background: #fff;
					border: 2rpx solid #fff;
					flex-shrink: 0;

					image {
						width: 138rpx;
						height: 138rpx;
						border-radius: 12rpx 12rpx 0 0;
						display: inline-block;
					}

					.text {
						background: #F6F6F6;
						display: flex;
						align-items: center;
						justify-content: center;
						overflow: hidden;
						text-overflow: ellipsis;
						border-radius: 0 0 12rpx 12rpx;
						padding: 0 8rpx;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
					}
				}
			}

			.bing {
				justify-content: space-between;

				.lists {
					min-width: 170rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					height: 60rpx;
					padding: 20rpx 36rpx;
					margin: 0;
					margin-bottom: 20rpx;
				}
			}

			.active {
				background: rgba(234, 61, 55, 0.08);
				border: 2rpx solid #DB3E3F !important;
				color: #DB3E3F;
				font-weight: bold;

				view {
					color: #DB3E3F;
					background: rgba(234, 61, 55, 0.08);
				}
			}
		}

		.mall_info {
			display: flex;
			justify-content: space-between;
			padding: 20rpx;
			padding-top: 40rpx;
			background: #fff;

			.infos {
				flex: 1;
				padding-left: 30rpx;
				display: flex;
				justify-content: space-between;
				flex-direction: column;

				.specs {
					color: #999;
					font-size: 24rpx;
				}

				.price {
					display: flex;
					align-items: flex-end;
					justify-content: flex-start;

					.big {
						font-size: 40rpx;
						color: #EA3D37;

						text {
							font-size: 24rpx;
						}
					}

					.small {
						font-size: 24rpx;
						color: #999999;
						text-decoration: line-through;

						text {
							font-size: 18rpx;
						}
					}
				}

				.sku_num {
					font-size: 24rpx;
					color: #999;
				}
			}

			image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 10rpx;
			}
		}
	}

	.history {
		padding: 20rpx 30rpx;
		background: #fff;

		.title {
			font-size: 28rpx;
		}

		.keyword_list {
			display: flex;
			align-items: center;
			justify-content: flex-start;
			flex-wrap: wrap;
			padding-top: 30rpx;

			.keyword {
				padding: 6rpx 10rpx;
				margin-bottom: 20rpx;
				background: #f5f5f5;
				margin-right: 20rpx;
			}
		}
	}

	.commodity_box {
		padding: 30rpx 20rpx;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.list {
			width: 344rpx;
			border-radius: 8rpx;
			padding: 12rpx;
			background: #FFFFFF;
			overflow: hidden;
			margin-bottom: 20rpx;

			.text_box {
				padding: 12rpx 0;
				position: relative;
				line-height: 2;
				padding-bottom: 0;

				.price_text {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.big {
						font-size: 40rpx;
						color: #EA3D37;

						text {
							font-size: 24rpx;
						}
					}

					.small {
						font-size: 24rpx;
						color: #999999;
						text-decoration: line-through;

						text {
							font-size: 16rpx;
						}
					}
				}

				.btn_boxs {
					display: flex;
					align-items: center;
					justify-content: space-between;
				}

				.addcart {
					width: 96rpx;
					height: 44rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					color: #fff;
					font-size: 20rpx;
					background: #EA3D37;
					border-radius: 6rpx;

				}

				.name {
					font-size: 28rpx;
					width: 320rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				.remake {
					font-size: 24rpx;
					color: #999;
				}

				.renzhen {
					font-size: 28rpx;
					color: #EA3D37;
				}
			}

			image {
				width: 320rpx;
				height: 248rpx;
				border-radius: 20rpx, 20rpx, 0rpx, 0rpx;

			}
		}
	}

	.container {
		background: #F5F5F5;
		min-height: 100vh;
	}

	.top_box {
		background: #fff;
		padding: 20rpx;

		.search_box {
			width: 710rpx;
			margin: 0 auto;
			height: 60rpx;
			border-radius: 30rpx;
			background: #FFFFFF;
			border: 2rpx solid #EA3D37;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-left: 28rpx;
			padding-right: 4rpx;

			input {
				flex: 1;
				padding: 0 20rpx;
			}

			.search_button {
				width: 110rpx;
				height: 48rpx;
				border-radius: 24rpx;
				background: #EA3D37;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				font-size: 24rpx;
			}
		}
	}
</style>